﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Hotel_application.Models;


namespace Hotel_application.Controllers
{
    public class BookingController : Controller
    {
        //
        // GET: /Booking/

        public ActionResult DatPhong()
        {
            var ks = new KhachSanEntities();
            
            return View(ks);
        }


        public ActionResult Payment()
        {            
            return View();
        }

        [HttpGet]
        public ActionResult  FindAvailableRoomType(string from, string to)
        {
            System.Threading.Thread.Sleep(800);
            string content = "";
            content += "<tr class='gkTableColor' style='background:none repeat scroll 0 0 #454545'>";
            content += "<td><strong>Phòng</strong></td>";
            content += "<td><strong>Loại phòng</strong></td>";
            content += "<td><strong>Hạng phòng</strong></td>";
            content += "<td><strong>Giá phòng</strong></td>";
            content += "<td><strong>Số lượng</strong></td>";
            content += "<td><strong>Thành tiền</strong></td></tr>";
            var from_date = Convert.ToDateTime(from);
            var to_date = Convert.ToDateTime(to);
            var ks = new KhachSanEntities();
            var res = ks.proc_LayDSLoaiHangPhongHoTro(from_date,to_date);
            int i = 0;
            bool checking = true;
            int count = 1;
            foreach (var l in res)
            {
                string tenLoai = ks.proc_LayTenLoaiPhong(l.maLoai).First();
                string tenHang = ks.proc_LayTenHangPhong(l.maHang).First();                
                content += "<tr><input type='hidden' id='typeID' name='maLoai" + i + "' value='" + l.maLoai + "'/>";
                if (checking)
                {
                    content += "<td rowspan='2'><img src='/Images/room_"+ count +".jpg' class='imgRoom' /></td>";
                    content += "<td rowspan='2'><div class='roomName'>" + tenLoai + "</div></td>";
                    checking = false;
                    count++;
                }
                else
                    checking = true;
                
                content += "<input type='hidden' id='rankID' name='maHang" + i + "' value='" + l.maHang + "'/>";
                content += "<td>" + tenHang + "</td>";
                content += "<input type='hidden' id='h-price" + i + "' name='price" + i + "' value='"+ l.giaPhong +"'/>";
                content += "<td style='color:#000' id='price" + i + "'>" + l.giaPhong.Value.ToString("#,###") + " VND" + "</td>";
                content += "<input type='hidden' id='quality" + i + "' name='quality" + i + "' value=''/>";
                content += "<td><select class='qualityRoom' id='" + i + "'>";
                content += "<option selected='selected'>0</option>";

                for (int j = 1; j <= l.soLuong; j++)
                {
                    content += "<option>" + j + "</option>";
                }
                
                content += "</select></td>";
                content += "<input type='hidden' id='h-total" + i + "' name='h-total" + i + "' value='0'><td style='color:#000' id='total" + i + "'>0 VND</td></tr>";
                i += 1;
            }
            content += "<tr style='color:#000;font-weight:bold' class='totalRow'><td colspan='4'><strong>Tổng cộng ( Giá tiền phòng / 1 đêm )</strong></td>";
            content += "<td><strong id='tongCongRoom'>0</strong></td>";
            content += "<td><strong id='tongCongTienRoom'>0 VND</strong></td></tr>";
            return Content(content);
        }

        [HttpPost]
        public ActionResult ReservationAction()
        {
            var ks = new KhachSanEntities();

            //lay maDP
            string max_maDP = ks.DatPhongs.Select(e => e.maDP).OrderByDescending(p => p).FirstOrDefault();
            max_maDP = max_maDP.Substring(2);
            int max_maDP_int = Convert.ToInt32(max_maDP);
            max_maDP_int += 1;
            max_maDP = Convert.ToString(max_maDP_int).PadLeft(7, '0');
            string maDP = "DP" + max_maDP;



            //lay ngay den, ngay di
            string timeIn = Request["timeIn"];
            string timeOut = Request["timeOut"];
            DateTime timeIn_dt = Convert.ToDateTime(timeIn);
            DateTime timeOut_dt = Convert.ToDateTime(timeOut);


            ///lay tong so phong
            int i = 0;
            int totalRoom = 0;
            while (Request["h-total" + i] != null)
            {

                string quantity = "quality" + i;
                string room = Request[quantity];
                if (room != "")
                {
                    totalRoom += Convert.ToInt16(room);
                }
                i++;
            }

            //them vao bang datphong
            DatPhong dp = new DatPhong();
            dp.maDP = maDP;
            dp.ngayBD = timeIn_dt;
            dp.ngayKT = timeOut_dt;
            dp.soLuongPhong = totalRoom;
            ks.DatPhongs.Add(dp);
            ks.SaveChanges();

            //them vao bang chi tiet dat phong
            i = 0;
            while (Request["h-total" + i] != null)
            {
                string quantity = "quality" + i;
                string room = Request[quantity];
                if (room != "")
                {
                    for (int j = 1; j <= Convert.ToInt16(room); j++)
                    {
                        //lay maDS
                        string max_maDS = ks.DanhSachDatPhongs.Select(e => e.maDS).OrderByDescending(p => p).FirstOrDefault();
                        max_maDS = max_maDS.Substring(2);
                        int max_maDS_int = Convert.ToInt32(max_maDS);
                        max_maDS_int += 1;
                        max_maDS = Convert.ToString(max_maDS_int).PadLeft(8, '0');
                        string maDS = "DS" + max_maDS;

                        //them vao bang chi tiet dat phong
                        string maLoai = Request["maLoai" + i];
                        string maHang = Request["maHang" + i];
                        string maPhong = ks.proc_LayMaPhong(maLoai, maHang, timeIn_dt, timeOut_dt).FirstOrDefault();
                        DanhSachDatPhong dsdp = new DanhSachDatPhong();
                        dsdp.maDP = maDP;
                        dsdp.maDS = maDS;
                        dsdp.maPhong = maPhong;
                        ks.DanhSachDatPhongs.Add(dsdp);
                        ks.SaveChanges();

                        //them vao bang trang thai phong
                        TrangThaiPhong ttp = new TrangThaiPhong();
                        ttp.maPhong = maPhong;
                        ttp.maTT = "TT02";
                        ttp.ngayBD = timeIn_dt;
                        ttp.ngayKT = timeOut_dt;
                        ks.TrangThaiPhongs.Add(ttp);
                        ks.SaveChanges();
                    }
                }
                i++;
            }

            //them thong tin vao bang datphong_canhan
            DatPhong_CaNhan dpcn = new DatPhong_CaNhan();
            dpcn.maDP = maDP;
            dpcn.hoTen = Request["hoTen"];
            dpcn.CMND = Request["CMND"];
            dpcn.ngaySinh = Convert.ToDateTime(Request["ngaySinh"]);
            dpcn.tenTKNH = Request["tenTK"];
            dpcn.maTKNH = Request["soTK"];
            ks.DatPhong_CaNhan.Add(dpcn);
            ks.SaveChanges();

            string[] infoPayment = new string[] { maDP, Request["CMND"], Request["total"] };
            object obj = infoPayment;

            return View("Payment", obj);

        }

    }

}
